﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Moneyman.Infrastructure;
using System.Data.Linq;
using System.Collections.Generic;
using Moneyman.Library.Helpers;
using Moneyman.Enums;
using System.ComponentModel;
using Moneyman.DataModel;

namespace Moneyman.ViewModel
{
    public class ViewModelBase : NotificationObject
    {
        #region Constructor
        public IDataContext Database;

        public ViewModelBase()
        {
            Database = new IDataContext(ConstKeys.ConnectionString);
            globalSettings.SettingsChanged -= globalSettings_SettingsChanged;
            globalSettings.SettingsChanged += globalSettings_SettingsChanged;
        } 
        #endregion

        #region Methods
        public virtual void Execute()
        {
            Database.SubmitChanges(ConflictMode.FailOnFirstConflict);
        }

        void globalSettings_SettingsChanged(object sender, EventArgs e)
        {
            RaisePropertyChanged(() => GlobalSettings);
        }
        #endregion

        #region EnumsCollection
        public IEnumerable<PaymentWayValue> EnumsOfPaymentWays
        {
            get
            {
                return EnumHelper.GetEnumValues<PaymentWayValue>();
            }
        }

        public IEnumerable<TransactionKindValue> EnumsOfTransactionKinds
        {
            get
            {
                return EnumHelper.GetEnumValues<TransactionKindValue>();
            }
        }

        public IEnumerable<CardKindValue> EnumsOfCardKinds
        {
            get
            {
                return EnumHelper.GetEnumValues<CardKindValue>();
            }
        }
        #endregion

        #region Common Properties
        public bool IsDesignMode
        {
            get
            {
                return DesignerProperties.IsInDesignTool;//(bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        private SettingsInfo globalSettings = new SettingsInfo();

        public SettingsInfo GlobalSettings
        {
            get { return globalSettings; }
            set
            {
                globalSettings = value;
                RaisePropertyChanged(() => GlobalSettings);
            }
        }
        #endregion
    }
}
